package com.kxmall.categorize.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.categorize.domain.HsWasteCategorize;
import com.kxmall.categorize.domain.bo.HsWasteCategorizeBo;
import com.kxmall.categorize.domain.vo.HsWasteCategorizeVo;
import com.kxmall.categorize.mapper.HsWasteCategorizeMapper;
import com.kxmall.categorize.service.IHsWasteCategorizeService;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.core.domain.PageQuery;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 废品分类信息Service业务层处理
 *
 * @author kxmall
 * @date 2023-11-21
 */
@RequiredArgsConstructor
@Service
public class HsWasteCategorizeServiceImpl implements IHsWasteCategorizeService {

    private final HsWasteCategorizeMapper baseMapper;

    /**
     * 查询废品分类信息
     */
    @Override
    public HsWasteCategorizeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询废品分类信息列表
     */
    @Override
    public TableDataInfo<HsWasteCategorizeVo> queryPageList(HsWasteCategorizeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HsWasteCategorize> lqw = buildQueryWrapper(bo);
        Page<HsWasteCategorizeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询废品分类信息列表
     */
    @Override
    public List<HsWasteCategorizeVo> queryList(HsWasteCategorizeBo bo) {
        LambdaQueryWrapper<HsWasteCategorize> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HsWasteCategorize> buildQueryWrapper(HsWasteCategorizeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HsWasteCategorize> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HsWasteCategorize::getName, bo.getName());
//        lqw.eq(StringUtils.isNotBlank(bo.getImgUrl()), HsWasteCategorize::getImgUrl, bo.getImgUrl());
        lqw.eq(bo.getParentId() != null, HsWasteCategorize::getParentId, bo.getParentId());
//        lqw.eq(bo.getCreateTime() != null, HsWasteCategorize::getCreateTime, bo.getCreateTime());
//        lqw.eq(bo.getUpdateTime() != null, HsWasteCategorize::getUpdateTime, bo.getUpdateTime());
        lqw.eq( HsWasteCategorize::getDeleteFlag, 0);
//        lqw.eq(bo.getDeleteFlag() != null, HsWasteCategorize::getDeleteFlag, bo.getDeleteFlag());
        return lqw;
    }

    /**
     * 新增废品分类信息
     */
    @Override
    public Boolean insertByBo(HsWasteCategorizeBo bo) {
        HsWasteCategorize add = BeanUtil.toBean(bo, HsWasteCategorize.class);
        add.setCreateTime(new Date());
        add.setUpdateTime(new Date());
        add.setDeleteFlag(0l);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改废品分类信息
     */
    @Override
    public Boolean updateByBo(HsWasteCategorizeBo bo) {
        HsWasteCategorize update = BeanUtil.toBean(bo, HsWasteCategorize.class);
        update.setDeleteFlag(0l);
        update.setUpdateTime(new Date());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HsWasteCategorize entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除废品分类信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        HsWasteCategorize hsWasteCategorize = new HsWasteCategorize();
        hsWasteCategorize.setDeleteFlag(1L);
//        return baseMapper.deleteBatchIds(ids) > 0;
        int num = baseMapper.update(hsWasteCategorize, new QueryWrapper<HsWasteCategorize>().in("id",ids));
        return num > 0;
    }

    @Override
    public List<HsWasteCategorizeVo> queryListByParentId(Long parentId) {
        if(parentId == null){
            return null;
        }
        LambdaQueryWrapper<HsWasteCategorize> lqw = Wrappers.lambdaQuery();
        lqw.eq( HsWasteCategorize::getParentId, parentId);
        lqw.eq( HsWasteCategorize::getDeleteFlag, 0);
        return baseMapper.selectVoList(lqw);
    }
}
